package com.xinyou.u8c.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyou.u8c.entity.*;
import com.xinyou.u8c.mapper.AssociatedCertificateMapper;
import com.xinyou.u8c.result.JsonResult;
import com.xinyou.u8c.service.*;
import com.xinyou.u8c.utils.CsvUtils;
import com.xinyou.u8c.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chq
 * @since 2023-08-31
 */
@Slf4j
@RestController
@RequestMapping("/u8c/associated-certificate")
public class AssociatedCertificateController {
    @Autowired
    private IAssociatedCertificateService associatedCertificateService;

    @Autowired
    private ISbmjService sbmjService;

    @Autowired
    private IDwsbService dwsbService;

    @Autowired
    private ISbndService sbndService;

    @Autowired
    private IFiglaccountingdocumentzjbService figlaccountingdocumentzjbService;

    @Autowired
    private IFiglaccdoccodezjbService figlaccdoccodezjbService;

    @Autowired
    private IFiglaccdocentryzjbService figlaccdocentryzjbService;

    @Autowired
    private IFiglaccdocassistancezjbService figlaccdocassistancezjbService;

    @Autowired
    private IFiglperiodstatezjbService figlperiodstatezjbService;

    @Autowired
    private IMDMPartnerService imdmPartnerService;

    @Autowired
    private IMDMPartnerAreaService imdmPartnerAreaService;

    @Autowired
    private IMDMPartnerTypeService imdmPartnerTypeService;

    @Autowired
    private IMDMAccountSetsService imdmAccountSetsService;

    @Autowired
    private IMDMLedgerService imdmLedgerService;

    @Autowired
    private IMDMAccountCalendarService imdmAccountCalendarService;

    @Autowired
    private IMDMAccountPeriodService imdmAccountPeriodService;

    @Autowired
    private IMDMSubjectSystemService imdmSubjectSystemService;

    @Autowired
    private IMDMSubjectNatureService imdmSubjectNatureService;

    @Autowired
    private IMDMAccountProTypeService imdmAccountProTypeServic;

    @Autowired
    private IMDMAccountProjectService imdmAccountProjectService;

    @Autowired
    private IMDMAccountAuxiliaryService imdmAccountAuxiliaryService;

    @Autowired
    private IMDMSubjectService imdmSubjectService;

    @Autowired
    private IMDMSettlementWayService imdmSettlementWayService;

    @Autowired
    private IMDMCodeSetTypeService imdmCodeSetTypeService;

    @Autowired
    private IMDMCodeSetsService imdmCodeSetsService;

    @Autowired
    private IMDMCodeItemsService imdmCodeItemsService;

    @Autowired
    private IMDMCurrencyService imdmCurrencyService;

    @Autowired
    private IMDMOrganizationService imdmOrganizationService;
    @Autowired
    private IMDMAssetsTypeZDYService iMDMAssetsTypeZDYService;

    @Autowired
    private IMDMFigdzjffZDYService imdmFigdzjffZDYService;

    @Autowired
    private IMDMAssettreatmentZDYService iMDMAssettreatmentZDYService;

    @Autowired
    private IMDMAssetusageZDYService imdmAssetusageZDYService;

    @Autowired
    private IMDMAssetsourceZDYService imdmAssetsourceZDYService;

    @Autowired
    private IMDMAmfinSourceZDYService imdmAmfinSourceZDYService;

    @Autowired
    private IGspsecuritylevelService iGspsecuritylevelService;

    @Autowired
    private IBfSettlementWayTypeService iBfSettlementWayTypeService;

    @Autowired
    private IMDMAccountOrgService imdmAccountOrgService;

    @Autowired
    private IMDMAccountRelationService imdmAccountRelationService;

    @Autowired
    private IMDMAdminOrgService imdmAdminOrgService;

    @Autowired
    private IMDMAssetusagestateZDYService imdmAssetusagestateZDYService;

    @Autowired
    private IImportLogService iImportLogService;

    @Autowired
    private IBfaccountingdepartmentzjbService bfaccountingdepartmentzjbService;

    @Autowired
    private IBfadminorganizationzjbService iBfadminorganizationzjbService;

    @Autowired
    private IBfaccountingemployeezjbService iBfaccountingemployeezjbService;

    @Autowired
    private IBfmasterorganizationzjbService iBfmasterorganizationzjbService;

    @Autowired
    private IBfemployeezjbService iBfemployeezjbService;

    @Autowired
    private IBfpartnerzjbService bfpartnerzjbService;

    @Autowired
    private IFiglaccdoctypezjbService iFiglaccdoctypezjbService;

    @Autowired
    private IBfcustomitemzjbService iBfcustomitemzjbService;

    @PostMapping("/getList")
    public JsonResult getList(VouchPagination pagination) {
        LambdaQueryWrapper<AssociatedCertificate> queryWrapper = new LambdaQueryWrapper();
        if (pagination.getIyear() != null) {
            queryWrapper.eq(AssociatedCertificate::getIyear, pagination.getIyear());
        }
        if (pagination.getIperiod() != null) {
            queryWrapper.eq(AssociatedCertificate::getIperiod, pagination.getIperiod());
        }
        if (pagination.getIsConvert() != null) {
            queryWrapper.eq(AssociatedCertificate::getIsConvert, pagination.getIsConvert());
        }
        if (pagination.getIsExport() != null) {
            queryWrapper.eq(AssociatedCertificate::getIsExport, pagination.getIsExport());
        }
        queryWrapper.orderByDesc(AssociatedCertificate::getIId);
        Page<AssociatedCertificate> pageWhere = new Page<>(pagination.getPage().longValue(), pagination.getPageSize().longValue());
        IPage<AssociatedCertificate> pageData = associatedCertificateService.page(pageWhere, queryWrapper);
        return new JsonResult(true, pageData);
    }

//    public List<Sbnd> getSbnd(String[] ids){
//        List<Sbnd> sbnds=associatedCertificateMapper.selectSbqj(ids);
//        List<Sbnd> sbndList=new ArrayList<>();
//        for (Sbnd sbnd : sbnds) {
//            if(Integer.parseInt(sbnd.getSbqj()) <= 12) {
//                sbnd.setSbnd(StringUtils.leftPad(sbnd.getSbqj(),2,'0'));
//                sbndList.add(sbnd);
//            }
//        }
//        return sbndList;
//    }

    @PostMapping("/importExcel")
    public JsonResult importExcel(@RequestBody MultipartFile file, Integer type) {
        ImportLog importLog = new ImportLog();
        importLog.setId(UUID.randomUUID().toString());
        importLog.setImportTime(new Date());
        importLog.setImportSource(file.toString());
        importLog.setType(type);
        try {
            switch (type) {
                case 1:
                    List<MDMPartner> mdmPartners = ExcelUtils.readMultipartFile(file, MDMPartner.class);
                    for (MDMPartner mdmPartner : mdmPartners) {
                        if(imdmPartnerService.getById(mdmPartner.getId())==null){
                            imdmPartnerService.save(mdmPartner);
                        }else {
                            imdmPartnerService.updateById(mdmPartner);
                        }
                    }
                    importLog.setImportTable("MDMPartner");
                    break;
                case 2:
                    List<MDMPartnerArea> mdmPartnerAreas = ExcelUtils.readMultipartFile(file, MDMPartnerArea.class);
                    for (MDMPartnerArea mdmPartnerArea : mdmPartnerAreas) {
                        if(imdmPartnerAreaService.getById(mdmPartnerArea.getId())==null){
                            imdmPartnerAreaService.save(mdmPartnerArea);
                        }else {
                            imdmPartnerAreaService.updateById(mdmPartnerArea);
                        }
                    }
                    importLog.setImportTable("MDMPartnerArea");
                    break;
                case 3:
                    List<MDMPartnerType> mdmPartnerTypes = ExcelUtils.readMultipartFile(file, MDMPartnerType.class);
                    for (MDMPartnerType mdmPartnerType : mdmPartnerTypes) {
                        if(imdmPartnerTypeService.getById(mdmPartnerType.getId())==null){
                            imdmPartnerTypeService.save(mdmPartnerType);
                        }else{
                            imdmPartnerTypeService.updateById(mdmPartnerType);
                        }
                    }
                    importLog.setImportTable("MDMPartnerType");
                    break;
                case 4:
                    List<MDMAccountSets> mdmAccountSets = ExcelUtils.readMultipartFile(file, MDMAccountSets.class);
                    for (MDMAccountSets mdmAccountSet : mdmAccountSets) {
                        if(imdmAccountSetsService.getById(mdmAccountSet.getId())==null){
                            imdmAccountSetsService.save(mdmAccountSet);
                        }else {
                            imdmAccountSetsService.updateById(mdmAccountSet);
                        }
                    }
                    importLog.setImportTable("MDMAccountSets");
                    break;
                case 5:
                    List<MDMLedger> mdmLedgers = ExcelUtils.readMultipartFile(file, MDMLedger.class);
                    for (MDMLedger mdmLedger : mdmLedgers) {
                        if(imdmLedgerService.getById(mdmLedger.getId())==null){
                            imdmLedgerService.save(mdmLedger);
                        }else {
                            imdmLedgerService.updateById(mdmLedger);
                        }
                    }
                    importLog.setImportTable("MDMLedger");
                    break;
                case 6:
                    List<MDMAccountCalendar> mdmAccountCalendars = ExcelUtils.readMultipartFile(file, MDMAccountCalendar.class);
                    for (MDMAccountCalendar mdmAccountCalendar : mdmAccountCalendars) {
                        if(imdmAccountCalendarService.getById(mdmAccountCalendar.getId())==null) {
                            imdmAccountCalendarService.save(mdmAccountCalendar);
                        }else {
                            imdmAccountCalendarService.updateById(mdmAccountCalendar);
                        }
                    }
                    importLog.setImportTable("MDMAccountCalendar");
                    break;
                case 7:
                    List<MDMAccountPeriod> mdmAccountPeriods = ExcelUtils.readMultipartFile(file, MDMAccountPeriod.class);
                    for (MDMAccountPeriod mdmAccountPeriod : mdmAccountPeriods) {
                        if(imdmAccountPeriodService.getById(mdmAccountPeriod.getId())==null){
                            imdmAccountPeriodService.save(mdmAccountPeriod);
                        }else {
                            imdmAccountPeriodService.updateById(mdmAccountPeriod);
                        }
                    }
                    importLog.setImportTable("MDMAccountPeriod");
                    break;
                case 8:
                    List<MDMSubjectSystem> mdmSubjectSystems = ExcelUtils.readMultipartFile(file, MDMSubjectSystem.class);
                    for (MDMSubjectSystem mdmSubjectSystem : mdmSubjectSystems) {
                        if(imdmSubjectSystemService.getById(mdmSubjectSystem.getId())==null){
                            imdmSubjectSystemService.save(mdmSubjectSystem);
                        }else {
                            imdmSubjectSystemService.updateById(mdmSubjectSystem);
                        }
                    }
                    importLog.setImportTable("MDMSubjectSystem");
                    break;
                case 9:
                    List<MDMSubjectNature> mdmSubjectNatures = ExcelUtils.readMultipartFile(file, MDMSubjectNature.class);
                    for (MDMSubjectNature mdmSubjectNature : mdmSubjectNatures) {
                        if(imdmSubjectNatureService.getById(mdmSubjectNature.getId())==null){
                            imdmSubjectNatureService.save(mdmSubjectNature);
                        }else {
                            imdmSubjectNatureService.updateById(mdmSubjectNature);
                        }
                    }
                    importLog.setImportTable("MDMSubjectNature");
                    break;
                case 10:
                    List<MDMAccountProType> mdmAccountProTypes = ExcelUtils.readMultipartFile(file, MDMAccountProType.class);
                    for (MDMAccountProType mdmAccountProType : mdmAccountProTypes) {
                        if(imdmAccountProTypeServic.getById(mdmAccountProType.getId())==null){
                            imdmAccountProTypeServic.save(mdmAccountProType);
                        }else {
                            imdmAccountProTypeServic.updateById(mdmAccountProType);
                        }
                    }
                    importLog.setImportTable("MDMAccountProType");
                    break;
                case 11:
                    for (MDMAccountProject mdmAccountProject : ExcelUtils.readMultipartFile(file, MDMAccountProject.class)) {
                        if(imdmAccountProjectService.getById(mdmAccountProject.getId())==null){
                            imdmAccountProjectService.save(mdmAccountProject);
                        }else {
                            imdmAccountProjectService.updateById(mdmAccountProject);
                        }
                    }
                    importLog.setImportTable("MDMAccountProject");
                    break;
                case 12:
                    List<MDMSubject> mdmSubjects = ExcelUtils.readMultipartFile(file, MDMSubject.class);
                    for (MDMSubject mdmSubject : mdmSubjects) {
                        if(imdmSubjectService.getById(mdmSubject.getId())==null){
                            imdmSubjectService.save(mdmSubject);
                        }else {
                            imdmSubjectService.updateById(mdmSubject);
                        }
                    }
                    importLog.setImportTable("MDMSubject");
                    break;
                case 13:
                    List<MDMAccountRelation> mdmAccountRelations = ExcelUtils.readMultipartFile(file, MDMAccountRelation.class);
                    for (MDMAccountRelation mdmAccountRelation : mdmAccountRelations) {
                        if(imdmAccountRelationService.getById(mdmAccountRelation.getId())==null){
                            imdmAccountRelationService.save(mdmAccountRelation);
                        }else {
                            imdmAccountRelationService.updateById(mdmAccountRelation);
                        }
                    }
                    importLog.setImportTable("MDMAccountRelation");
                    break;
                case 14:
                    List<MDMAccountAuxiliary> mdmAccountAuxiliaries = ExcelUtils.readMultipartFile(file, MDMAccountAuxiliary.class);
                    for (MDMAccountAuxiliary mdmAccountAuxiliary : mdmAccountAuxiliaries) {
                        if(imdmAccountAuxiliaryService.getById(mdmAccountAuxiliary.getId())==null){
                            imdmAccountAuxiliaryService.save(mdmAccountAuxiliary);
                        }else {
                            imdmAccountAuxiliaryService.updateById(mdmAccountAuxiliary);
                        }
                    }
                    importLog.setImportTable("MDMAccountAuxiliary");
                    break;
                case 15:
                    List<MDMSettlementWay> mdmSettlementWays = ExcelUtils.readMultipartFile(file, MDMSettlementWay.class);
                    for (MDMSettlementWay mdmSettlementWay : mdmSettlementWays) {
                        if(imdmSettlementWayService.getById(mdmSettlementWay.getId())==null){
                            imdmSettlementWayService.save(mdmSettlementWay);
                        }else {
                            imdmSettlementWayService.updateById(mdmSettlementWay);
                        }
                    }
                    importLog.setImportTable("MDMSettlementWay");
                    break;
                case 16:
                    List<MDMCodeSetType> mdmCodeSetTypes = ExcelUtils.readMultipartFile(file, MDMCodeSetType.class);
                    for (MDMCodeSetType mdmCodeSetType : mdmCodeSetTypes) {
                        if(imdmCodeSetTypeService.getById(mdmCodeSetType.getId())==null){
                            imdmCodeSetTypeService.save(mdmCodeSetType);
                        }else {
                            imdmCodeSetTypeService.updateById(mdmCodeSetType);
                        }
                    }
                    importLog.setImportTable("MDMCodeSetType");
                    break;
                case 17:
                    List<MDMCodeSets> mdmCodeSets = ExcelUtils.readMultipartFile(file, MDMCodeSets.class);
                    for (MDMCodeSets mdmCodeSet : mdmCodeSets) {
                        if(imdmCodeSetsService.getById(mdmCodeSet.getId())==null){
                            imdmCodeSetsService.save(mdmCodeSet);
                        }else {
                            imdmCodeSetsService.updateById(mdmCodeSet);
                        }
                    }
                    importLog.setImportTable("MDMCodeSets");
                    break;
                case 18:
                    List<MDMCodeItems> mdmCodeItems = ExcelUtils.readMultipartFile(file, MDMCodeItems.class);
                    for (MDMCodeItems mdmCodeItem : mdmCodeItems) {
                        if(imdmCodeItemsService.getById(mdmCodeItem.getId())==null){
                            imdmCodeItemsService.save(mdmCodeItem);
                        }else {
                            imdmCodeItemsService.updateById(mdmCodeItem);
                        }
                    }
                    importLog.setImportTable("MDMCodeItems");
                    break;
                case 19:
                    List<MDMCurrency> mdmCurrencies = ExcelUtils.readMultipartFile(file, MDMCurrency.class);
                    for (MDMCurrency mdmCurrency : mdmCurrencies) {
                        if(imdmCodeItemsService.getById(mdmCurrency.getId())==null){
                            imdmCurrencyService.save(mdmCurrency);
                        }else {
                            imdmCurrencyService.updateById(mdmCurrency);
                        }
                    }
                    importLog.setImportTable("MDMCurrency");
                    break;
                case 20:
                    List<MDMOrganization> mdmOrganizations = ExcelUtils.readMultipartFile(file, MDMOrganization.class);
                    for (MDMOrganization mdmOrganization : mdmOrganizations) {
                        if(imdmOrganizationService.getById(mdmOrganization.getId())==null){
                            imdmOrganizationService.save(mdmOrganization);
                        }else {
                            imdmOrganizationService.updateById(mdmOrganization);
                        }
                    }
                    importLog.setImportTable("MDMOrganization");
                    break;
                case 21:
                    List<MDMAdminOrg> mdmAdminOrgs = ExcelUtils.readMultipartFile(file, MDMAdminOrg.class);
                    for (MDMAdminOrg mdmAdminOrg : mdmAdminOrgs) {
                        if(imdmAdminOrgService.getById(mdmAdminOrg.getId())==null){
                            imdmAdminOrgService.save(mdmAdminOrg);
                        }else {
                            imdmAdminOrgService.updateById(mdmAdminOrg);
                        }
                    }
                    importLog.setImportTable("MDMAdminOrg");
                    break;
                case 22:
                    List<MDMAccountOrg> mdmAccountOrgs = ExcelUtils.readMultipartFile(file, MDMAccountOrg.class);
                    for (MDMAccountOrg mdmAccountOrg : mdmAccountOrgs) {
                        if(imdmAccountOrgService.getById(mdmAccountOrg.getId())==null){
                            imdmAccountOrgService.save(mdmAccountOrg);
                        }else {
                            imdmAccountOrgService.updateById(mdmAccountOrg);
                        }
                    }
                    importLog.setImportTable("MDMAccountOrg");
                    break;
                case 23:
                    List<MDMAssetsTypeZDY> mdmAssetsTypeZDIES = ExcelUtils.readMultipartFile(file, MDMAssetsTypeZDY.class);
                    for (MDMAssetsTypeZDY mdmAssetsTypeZDY : mdmAssetsTypeZDIES) {
                        if (iMDMAssetsTypeZDYService.getById(mdmAssetsTypeZDY.getId()) == null) {
                            iMDMAssetsTypeZDYService.save(mdmAssetsTypeZDY);
                        }else {
                            iMDMAssetsTypeZDYService.updateById(mdmAssetsTypeZDY);
                        }
                    }
                    importLog.setImportTable("MDMAssetsTypeZDY");
                    break;
                case 24:
                    List<MDMFigdzjffZDY> mdmFigdzjffZDIES = ExcelUtils.readMultipartFile(file, MDMFigdzjffZDY.class);
                    for (MDMFigdzjffZDY mdmFigdzjffZDY : mdmFigdzjffZDIES) {
                        if(imdmFigdzjffZDYService.getById(mdmFigdzjffZDY.getId())==null){
                            imdmFigdzjffZDYService.save(mdmFigdzjffZDY);
                        }else {
                            imdmFigdzjffZDYService.updateById(mdmFigdzjffZDY);
                        }
                    }
                    importLog.setImportTable("MDMFigdzjffZDY");
                    break;
                case 25:
                    List<MDMAssetusagestateZDY> mdmAssetusagestateZDIES = ExcelUtils.readMultipartFile(file, MDMAssetusagestateZDY.class);
                    for (MDMAssetusagestateZDY mdmAssetusagestateZDY : mdmAssetusagestateZDIES) {
                        if(imdmAssetusagestateZDYService.getById(mdmAssetusagestateZDY.getId())==null){
                            imdmAssetusagestateZDYService.save(mdmAssetusagestateZDY);
                        }else {
                            imdmAssetusagestateZDYService.updateById(mdmAssetusagestateZDY);
                        }
                    }

                    importLog.setImportTable("MDMAssetusagestateZDY");
                    break;
                case 26:
                    List<MDMAssettreatmentZDY> mdmAssettreatmentZDYS = ExcelUtils.readMultipartFile(file, MDMAssettreatmentZDY.class);
                    for (MDMAssettreatmentZDY mdmAssettreatmentZDY : mdmAssettreatmentZDYS) {
                        if(iMDMAssettreatmentZDYService.getById(mdmAssettreatmentZDY.getId())==null){
                            iMDMAssettreatmentZDYService.save(mdmAssettreatmentZDY);
                        }else {
                            iMDMAssettreatmentZDYService.updateById(mdmAssettreatmentZDY);
                        }
                    }
                    importLog.setImportTable("MDMAssettreatmentZDY");
                    break;
                case 27:
                    List<MDMAssetusageZDY> mdmAssetusageZDIES = ExcelUtils.readMultipartFile(file, MDMAssetusageZDY.class);
                    for (MDMAssetusageZDY mdmAssetusageZDY : mdmAssetusageZDIES) {
                        if(imdmAssetusageZDYService.getById(mdmAssetusageZDY.getId())==null){
                            imdmAssetusageZDYService.save(mdmAssetusageZDY);
                        }else {
                            imdmAssetusageZDYService.updateById(mdmAssetusageZDY);
                        }
                    }
                    importLog.setImportTable("MDMAssetusageZDY");
                    break;
                case 28:
                    List<MDMAssetsourceZDY> mdmAssetsourceZDIES = ExcelUtils.readMultipartFile(file, MDMAssetsourceZDY.class);
                    for (MDMAssetsourceZDY mdmAssetsourceZDY : mdmAssetsourceZDIES) {
                        if (imdmAssetsourceZDYService.getById(mdmAssetsourceZDY.getId())==null){
                            imdmAssetsourceZDYService.save(mdmAssetsourceZDY);
                        }else {
                            imdmAssetsourceZDYService.updateById(mdmAssetsourceZDY);
                        }
                    }
                    importLog.setImportTable("MDMAssetsourceZDY");
                    break;
                case 29:
                    List<MDMAmfinSourceZDY> mdmAmfinSourceZDIES = ExcelUtils.readMultipartFile(file, MDMAmfinSourceZDY.class);
                    for (MDMAmfinSourceZDY mdmAmfinSourceZDY : mdmAmfinSourceZDIES) {
                        if(imdmAmfinSourceZDYService.getById(mdmAmfinSourceZDY.getId())==null){
                            imdmAmfinSourceZDYService.save(mdmAmfinSourceZDY);
                        }else {
                            imdmAmfinSourceZDYService.updateById(mdmAmfinSourceZDY);
                        }
                    }
                    importLog.setImportTable("MDMAmfinSourceZDY");
                    break;
                case 30:
                    List<Gspsecuritylevel> gspsecuritylevels = ExcelUtils.readMultipartFile(file, Gspsecuritylevel.class);
                    for (Gspsecuritylevel gspsecuritylevel : gspsecuritylevels) {
                        if(iGspsecuritylevelService.getById(gspsecuritylevel.getId())==null){
                            iGspsecuritylevelService.save(gspsecuritylevel);
                        }else {
                            iGspsecuritylevelService.updateById(gspsecuritylevel);
                        }
                    }
                    importLog.setImportTable("Gspsecuritylevel");
                    break;
                case 31:
                    List<BfSettlementWayType> bfSettlementWayTypes = ExcelUtils.readMultipartFile(file, BfSettlementWayType.class);
                    for (BfSettlementWayType bfSettlementWayType : bfSettlementWayTypes) {
                        if(iBfSettlementWayTypeService.getById(bfSettlementWayType.getId())==null){
                            iBfSettlementWayTypeService.save(bfSettlementWayType);
                        }else {
                            iBfSettlementWayTypeService.updateById(bfSettlementWayType);
                        }
                    }
                    importLog.setImportTable("BfSettlementWayType");
                    break;
                default:
                    importLog.setIsSuccess(0);
                    iImportLogService.save(importLog);
                    return new JsonResult(false, "传参错误，请联系管理员");
            }
            importLog.setIsSuccess(1);
            iImportLogService.save(importLog);
            return new JsonResult(true, "导入成功");
        }catch (Exception e){
            e.printStackTrace();
            importLog.setIsSuccess(0);
            importLog.setFailReason(e.toString());
            iImportLogService.save(importLog);
            return new JsonResult(false, e);
        }
    }

    /**
     * 导出CSV文件
     */
    @GetMapping("/downloadCsv/{ids}")
    public ResponseEntity<byte[]> downloadFile(HttpServletRequest request, HttpServletResponse response, HttpSession session,@PathVariable List<String> ids) throws IOException {
        List<AssociatedCertificate> glAccvouches = associatedCertificateService.list(new LambdaQueryWrapper<AssociatedCertificate>().in(AssociatedCertificate::getIId,ids));
        List<Figlaccountingdocumentzjb> figlaccountingdocumentzjbs =new ArrayList<>();
        List<Figlaccdoccodezjb> figlaccdoccodezjbs =new ArrayList<>();
        List<Figlaccdocentryzjb> figlaccdocentryzjbs = new ArrayList<>();
        List<Figlaccdocassistancezjb> figlaccdocassistancezjbs = new ArrayList<>();
        for (AssociatedCertificate glAccvouch : glAccvouches) {
            figlaccdoccodezjbs.add(figlaccdoccodezjbService.getById(glAccvouch.getFiglaccdoccodezjbId()));
            figlaccountingdocumentzjbs.addAll(figlaccountingdocumentzjbService.listByIds(Arrays.asList(glAccvouch.getFiglaccountingdocumentzjbId().split(";"))));
            if(glAccvouch.getFiglaccdocentryzjbId()!=null){
                figlaccdocentryzjbs.addAll(figlaccdocentryzjbService.listByIds(Arrays.asList(glAccvouch.getFiglaccdocentryzjbId().split(";"))));
            }
            if (glAccvouch.getFiglaccdocassistancezjbId() != null) {
                figlaccdocassistancezjbs.addAll(figlaccdocassistancezjbService.listByIds(Arrays.asList(glAccvouch.getFiglaccdocassistancezjbId().split(";"))));
            }
            glAccvouch.setIsExport("1");
            associatedCertificateService.updateById(glAccvouch);
        }
        Calendar calendar= Calendar.getInstance();
        //上报密级
        Sbmj sbmj = new Sbmj();
        List<List<String>> sbmjList = makeSheetDataList(sbmjService.list(), sbmj.getFields(), Sbmj.class);
        File sbmjFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】SBMJ","GB2312", sbmj.getHeader(), sbmjList);
        //上报单位
        Dwsb dwsb = new Dwsb();
        List<List<String>> dwsbList = makeSheetDataList(dwsbService.list(), dwsb.getFields(), Dwsb.class);
        File dwsbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】DWSB","GB2312", dwsb.getHeader(), dwsbList);
        //上报年度
        List<Sbnd> sbnds=new ArrayList<>();
        Sbnd sbnd = new Sbnd();
        sbnd.setSbnd(String.valueOf(calendar.get(Calendar.YEAR)));
        sbnd.setSbqj(StringUtils.leftPad(String.valueOf(calendar.get(Calendar.MONTH)),2, "0"));
        sbnds.add(sbnd);
        List<List<String>> sbndList = makeSheetDataList(sbnds, sbnd.getFields(), Sbnd.class);
        File sbndFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】SBND","GB2312", sbnd.getHeader(), sbndList);
        //凭证头
        Figlaccountingdocumentzjb figlaccountingdocumentzjb = new Figlaccountingdocumentzjb();
        List<List<String>> figlaccountingdocumentzjbList = makeSheetDataList(figlaccountingdocumentzjbs, figlaccountingdocumentzjb.getFields(), Figlaccountingdocumentzjb.class);
        File figlaccountingdocumentzjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】FIGLACCOUNTINGDOCUMENTZJB","GB2312", figlaccountingdocumentzjb.getHeader(), figlaccountingdocumentzjbList);
        //凭证编号文件
        Figlaccdoccodezjb figlaccdoccodezjb = new Figlaccdoccodezjb();
        List<List<String>> figlaccdoccodezjbList = makeSheetDataList(figlaccdoccodezjbs, figlaccdoccodezjb.getFields(), Figlaccdoccodezjb.class);
        File figlaccdoccodezjbfile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】FIGLACCDOCCODEZJB", "GB2312", figlaccdoccodezjb.getHeader(), figlaccdoccodezjbList);
        //凭证分录文件
        Figlaccdocentryzjb figlaccdocentryzjb = new Figlaccdocentryzjb();
        List<List<String>> figlaccdocentryzjbList = makeSheetDataList(figlaccdocentryzjbs, figlaccdocentryzjb.getFields(), Figlaccdocentryzjb.class);
        File figlaccdocentryzjbfile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】FIGLACCDOCENTRYZJB", "GB2312", figlaccdocentryzjb.getHeader(), figlaccdocentryzjbList);
        //凭证辅助文件
        Figlaccdocassistancezjb figlaccdocassistancezjb = new Figlaccdocassistancezjb();
        List<List<String>> figlaccdocassistancezjbList = makeSheetDataList(figlaccdocassistancezjbs, figlaccdocassistancezjb.getFields(), Figlaccdocassistancezjb.class);
        File figlaccdocassistancezjbfile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】FIGLACCDOCASSISTANCEZJB", "GB2312", figlaccdocassistancezjb.getHeader(), figlaccdocassistancezjbList);
        //期间状态文件
        Figlperiodstatezjb figlperiodstatezjb = new Figlperiodstatezjb();
        List<List<String>> figlperiodstatezjbList = makeSheetDataList(figlperiodstatezjbService.list(), figlperiodstatezjb.getFields(), Figlperiodstatezjb.class);
        File figlperiodstatezjbfile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】FIGLPERIODSTATEZJB", "GB2312", figlperiodstatezjb.getHeader(), figlperiodstatezjbList);
        //核算部门
        Bfaccountingdepartmentzjb bfaccountingdepartmentzjb = new Bfaccountingdepartmentzjb();
        List<List<String>> accountingdepartmentList = makeSheetDataList(bfaccountingdepartmentzjbService.list(), bfaccountingdepartmentzjb.getFields(), Bfaccountingdepartmentzjb.class);
        File accountingdepartmentFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFACCOUNTINGDEPARTMENTZJB","GB2312", bfaccountingdepartmentzjb.getHeader(), accountingdepartmentList);
        //行政部门
        Bfadminorganizationzjb bfadminorganizationzjb = new Bfadminorganizationzjb();
        List<List<String>> bfadminorganizationzjbList = makeSheetDataList(iBfadminorganizationzjbService.list(), bfadminorganizationzjb.getFields(), Bfadminorganizationzjb.class);
        File bfadminorganizationzjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFADMINORGANIZATIONZJB","GB2312", bfadminorganizationzjb.getHeader(), bfadminorganizationzjbList);
        //组织机构部门
        Bfmasterorganizationzjb bfmasterorganizationzjb = new Bfmasterorganizationzjb();
        List<List<String>> bfmasterorganizationzjbList = makeSheetDataList(iBfmasterorganizationzjbService.list(), bfmasterorganizationzjb.getFields(), Bfmasterorganizationzjb.class);
        File bfmasterorganizationzjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFMASTERORGANIZATIONZJB","GB2312", bfmasterorganizationzjb.getHeader(), bfmasterorganizationzjbList);
        //核算人员
        Bfaccountingemployeezjb bfaccountingemployeezjb = new Bfaccountingemployeezjb();
        List<List<String>> bfaccountingemployeezjbList = makeSheetDataList(iBfaccountingemployeezjbService.list(), bfaccountingemployeezjb.getFields(), Bfaccountingemployeezjb.class);
        File bfaccountingemployeezjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFACCOUNTINGEMPLOYEEZJB","GB2312", bfaccountingemployeezjb.getHeader(), bfaccountingemployeezjbList);
        //核算人员
        Bfemployeezjb bfemployeezjb = new Bfemployeezjb();
        List<List<String>> bfemployeezjbList = makeSheetDataList(iBfemployeezjbService.list(), bfemployeezjb.getFields(), Bfemployeezjb.class);
        File bfemployeezjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFEMPLOYEEZJB","GB2312", bfemployeezjb.getHeader(), bfemployeezjbList);
        //组织往来单位
        Bfpartnerzjb bfpartnerzjb = new Bfpartnerzjb();
        List<List<String>> bfpartnerzjbList = makeSheetDataList(bfpartnerzjbService.list(), bfpartnerzjb.getFields(), Bfpartnerzjb.class);
        File bfpartnerzjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFPARTNERZJB","GB2312", bfpartnerzjb.getHeader(), bfpartnerzjbList);
        //组织往来单位
        Figlaccdoctypezjb figlaccdoctypezjb = new Figlaccdoctypezjb();
        List<List<String>> figlaccdoctypezjbList = makeSheetDataList(iFiglaccdoctypezjbService.list(), figlaccdoctypezjb.getFields(), Figlaccdoctypezjb.class);
        File figlaccdoctypezjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】FIGLACCDOCTYPEZJB","GB2312", figlaccdoctypezjb.getHeader(), figlaccdoctypezjbList);
        //组织往来单位
        Bfcustomitemzjb bfcustomitemzjb = new Bfcustomitemzjb();
        List<List<String>> bfcustomitemzjbList = makeSheetDataList(iBfcustomitemzjbService.list(), bfcustomitemzjb.getFields(), Bfcustomitemzjb.class);
        File bfcustomitemzjbFile = CsvUtils.writeToCsv(request.getServletContext().getRealPath("download"), session.getId(), "【公开】BFCUSTOMITEMZJB","GB2312", bfcustomitemzjb.getHeader(), bfcustomitemzjbList);
        //压缩成ZIP文件
        List<File> files = new ArrayList<File>();
        getAllFiles(sbmjFile, dwsbFile, sbndFile, figlaccdocassistancezjbfile, figlaccountingdocumentzjbFile, figlaccdoccodezjbfile, figlaccdocentryzjbfile, figlperiodstatezjbfile, files);
        getAllFiles(accountingdepartmentFile, bfadminorganizationzjbFile, bfmasterorganizationzjbFile, bfaccountingemployeezjbFile, bfemployeezjbFile, bfpartnerzjbFile, figlaccdoctypezjbFile, bfcustomitemzjbFile, files);
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        File writeToZip = CsvUtils.writeToZip(request.getServletContext().getRealPath("download"), session.getId(), "【公开】【2023】【"+Calendar.getInstance().get(Calendar.MONTH) +"】【915201142144010880】【贵州永红航空机械有限责任公司】"+dateFormat.format(calendar.getTime()), files);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + writeToZip.getName());
        return new ResponseEntity<>(FileUtils.readFileToByteArray(writeToZip), headers, HttpStatus.OK);
    }

    private void getAllFiles(File accountingdepartmentFile, File bfadminorganizationzjbFile, File bfmasterorganizationzjbFile, File bfaccountingemployeezjbFile, File bfemployeezjbFile, File bfpartnerzjbFile, File figlaccdoctypezjbFile, File bfcustomitemzjbFile, List<File> files) {
        files.add(accountingdepartmentFile);
        files.add(bfadminorganizationzjbFile);
        files.add(bfmasterorganizationzjbFile);
        files.add(bfemployeezjbFile);
        files.add(bfpartnerzjbFile);
        files.add(figlaccdoctypezjbFile);
        files.add(bfcustomitemzjbFile);
        files.add(bfaccountingemployeezjbFile);
    }


    public static List<List<String>> makeSheetDataList(List<?> list, String[] fields, Class<?> classType) {
        List<List<String>> dataList = new ArrayList<>();
        String temp = JSONObject.toJSONString(list);
        List<?> objects = JSONArray.parseArray(temp, classType);
        for (Object object : objects) {
            Map<String, String> map = JSONObject.parseObject(JSONObject.toJSONString(object),
                    new TypeReference<Map<String, String>>() {
                    });
            List<String> tempList = new ArrayList<>();
            for (String field : fields) {
                tempList.add(map.get(field));
            }
            dataList.add(tempList);
        }
        return dataList;
    }


}
